//
//  BIDCreateMeetingRemindViewController.m
//  cxgov
//
//  Created by mal on 16/3/15.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDCreateMeetingRemindViewController.h"
#import "BIDCustomTextField.h"
#import "BIDCustomTextView.h"
#import "BIDCustomDateView.h"
#import "BIDMemberListViewController.h"

//创建会议提醒
static NSString *createMeetingRemindURL = @"meetingAndroid!addMeeting.action";

@interface BIDCreateMeetingRemindViewController ()<BIDCustomDateViewDelegate, UITextFieldDelegate, BIDMemberListViewControllerDelegate, UIAlertViewDelegate>
{
    NSString *_memberIds;
}
@end

@implementation BIDCreateMeetingRemindViewController
@synthesize meetingContent;
@synthesize meetingPlace;
@synthesize meetingSubject;
@synthesize meetingTime;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建提醒";
    // Do any additional setup after loading the view from its nib.
    _contentTextView.text = meetingContent;
    _subjectTF.text = meetingSubject;
    _timeTF.text = meetingTime;
    _placeTF.text = meetingPlace;
    //
    [self prepareForSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSubViews
{
    _memberTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _memberTF.layer.borderWidth = 1.f;
    _memberTF.inputAccessoryView = self.toolBar;
    _memberTF.layer.cornerRadius = 3.f;
    _memberTF.clipsToBounds = YES;
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar.png"]];
    _memberTF.rightView = imgView;
    _memberTF.rightViewMode = UITextFieldViewModeAlways;
    //
    _subjectTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _subjectTF.layer.borderWidth = 1.f;
    _subjectTF.inputAccessoryView = self.toolBar;
    _subjectTF.layer.cornerRadius = 3.f;
    _subjectTF.clipsToBounds = YES;
    //
    _timeTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _timeTF.layer.borderWidth = 1.f;
    _timeTF.inputAccessoryView = self.toolBar;
    _timeTF.layer.cornerRadius = 3.f;
    _timeTF.clipsToBounds = YES;
    //
    _placeTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _placeTF.layer.borderWidth = 1.f;
    _placeTF.inputAccessoryView = self.toolBar;
    _placeTF.layer.cornerRadius = 3.f;
    _placeTF.clipsToBounds = YES;
    //
    _remindTimeTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _remindTimeTF.layer.borderWidth = 1.f;
    _remindTimeTF.layer.cornerRadius = 3.f;
    _remindTimeTF.clipsToBounds = YES;
    BIDCustomDateView *dateView = [[BIDCustomDateView alloc] initWithMode:3];
    dateView.delegate = self;
    dateView.bIsInputView = YES;
    _remindTimeTF.inputView = dateView;
    //
    _contentTextView.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _contentTextView.layer.borderWidth = 1.f;
    _contentTextView.inputAccessoryView = self.toolBar;
    _contentTextView.layer.cornerRadius = 3.f;
    _contentTextView.clipsToBounds = YES;
    _contentTextView.placeholder = @"请输入会议内容";
}

//选择人员
- (IBAction)chooseMemberBtnHandler:(id)sender
{
    BIDMemberListViewController *vc = [[BIDMemberListViewController alloc] init];
    vc.chooseType = TYPE_CHECK;
    vc.delegate = self;
    vc.bShowAllOption = YES;
    vc.rightNavigationItemTitle = @"确定";
    vc.choosedMemberIdArr = [_memberIds componentsSeparatedByString:@","];
    [self.navigationController pushViewController:vc animated:YES];
}

//确认
- (IBAction)confirmBtnHandler:(id)sender
{
    if(_memberIds.length==0)
    {
        [BIDGlobalMethods showAlertView:@"请选择参会人员" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
        return;
    }
    NSString *strTitle = _subjectTF.text;
    NSString *strTime = _timeTF.text;
    NSString *strPlace = _placeTF.text;
    NSString *strRemindTime = _remindTimeTF.text;
    if(strTitle.length==0||strTime.length==0||strPlace.length==0||strRemindTime.length==0)
    {
        [BIDGlobalMethods showAlertView:@"会议主题、会议时间、会议地点和提醒时间不能为空" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
        return;
    }
    NSString *strContent = _contentTextView.text;
    if(strContent.length==0)
    {
        [BIDGlobalMethods showAlertView:@"请输入会议内容" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
        return;
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], createMeetingRemindURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"title=%@&content=%@&meetingTime=%@&warnTime=%@&address=%@&ids=%@&userId=%@", strTitle, strContent, strTime, strRemindTime, strPlace, _memberIds, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue] == 1)
            {
                [BIDGlobalMethods showAlertView:@"新建成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"新建失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - BIDMemberListViewControllerDelegate
- (void)getChoosedMemberIdForNotice:(NSString *)ids andNames:(NSString *)names
{
    _memberIds = ids;
    _memberTF.text = names;
}

#pragma mark - BIDCustomDateViewDelegate
- (void)getDate:(NSString *)strDate
{
    _remindTimeTF.text = strDate;
}

@end
